//
//  InvestigateViewController.m
//  PASystem
//
//   先遣团考察
//   
//


#import "InvestigateViewController.h"
#import "InvestigateModel.h"
#import "InvestigateCell.h"


@interface InvestigateViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSMutableArray *dataArray2;

@property (nonatomic,strong) UITableView *tab1;

@property (nonatomic,strong) UITableView *tab2;
@end

@implementation InvestigateViewController

static NSString *const ID = @"InvestigateCell";

- (NSMutableArray *)dataArray2{
    if (_dataArray2 == nil) {
        
        _dataArray2 = [NSMutableArray array];
    }
    return _dataArray2;
}

-(NSMutableArray *)dataArray{
    
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:241/255.0 green:242/255.0 blue:243/255.0 alpha:1];
    
    UITableView *tab1 = [[UITableView alloc] init];
    self.tab1 = tab1;
    tab1.delegate = self;
    tab1.dataSource = self;
    tab1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tab1];
    [tab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).mas_offset(10);
        make.left.equalTo(self.view.mas_left).mas_offset(10);
        make.right.equalTo(self.view.mas_right).mas_offset(-10);
        make.height.equalTo(@200);
    }];
    
    UITableView *tab2 = [[UITableView alloc] init];
    self.tab2 = tab2;
    tab2.delegate = self;
    tab2.dataSource = self;
    tab2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tab2];
    [tab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tab1.mas_bottom).mas_offset(10);
        make.left.equalTo(self.view.mas_left).mas_offset(10);
        make.right.equalTo(self.view.mas_right).mas_offset(-10);
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-148);
    }];
    
    [self loadData];
    
    [self loadData2];
}

- (void)loadData{

    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': kcxxMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        NSDictionary *dict = JSON[@"reason"];

        if (dict) {
            NSDictionary *dict2 = dict[@"attrs"];

            InvestigateModel *model = [InvestigateModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray addObject:model];
        }
        [self.tab1 reloadData];

    } failure:^(NSError *error) {
        
        
    }];
}

- (void)loadData2{

    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json  = [NSString stringWithFormat:@"{'action': cyxxMch,'service': DXMService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.ID];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {

        NSArray *arr = JSON[@"reason"];
        
        if (arr.count) {
            for (NSDictionary *dict in arr) {
                
                NSDictionary *dict2 = dict[@"attrs"];
                
                InvestigateModel *model = [InvestigateModel mj_objectWithKeyValues:dict2];
                
                [self.dataArray2 addObject:model];
                
            }
  
        }
 
        [self.tab2 reloadData];

    } failure:^(NSError *error) {
        
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.tab1) {
        
        return 3;
        
    }else{
        return self.dataArray2.count;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.tab1) {
        
        InvestigateModel *model = self.dataArray.firstObject;
        
        if (indexPath.row == 0) {
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10,80, 40)];
            
            label.text = @"考察内容:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            lbl2.text = model.CONTENTS;
            lbl2.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
            
        }else if (indexPath.row == 1){
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            
            label.text = @"考察地点:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            lbl2.textAlignment = NSTextAlignmentRight;
            
            lbl2.text = model.ADDRESS;
            
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            
            return cell;
            
            
        }
        else {
            
            UITableViewCell* cell = [[UITableViewCell alloc]init];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 40)];
            
            label.text = @"出发时间:";
            
            label.font = [UIFont systemFontOfSize:15];
            
            [cell.contentView addSubview:label];
            
            UILabel *lbl2 = [[UILabel alloc] init];
            
            lbl2.font = [UIFont systemFontOfSize:15];
            
            //时间戳
            NSString *string = model.TIME;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"MMM d, yyyy HH:mm:ss aa";
            fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];
            NSDate *date = [fmt dateFromString:string];
            NSDateFormatter *fm = [[NSDateFormatter alloc] init];
            fm.dateFormat = @"yyyy-MM-dd";
            NSString *selfYear = [fm stringFromDate:date];
            
            lbl2.text = selfYear;
            
            lbl2.textAlignment = NSTextAlignmentRight;
            
            [cell.contentView addSubview:lbl2];
            
            [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(label.mas_centerY);
                make.left.equalTo(label.mas_right);
                make.height.equalTo(label.mas_height);
                make.right.equalTo(cell.contentView.mas_right).mas_offset(-5);
            }];
            
            return cell;
            
        }
        
    }else{
        InvestigateModel *model = self.dataArray2[indexPath.row];
        
        InvestigateCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            
            cell = [InvestigateCell WithInvestigateCell];
        }
        
        cell.model = model;
        
        return cell;
    }


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    InvestigateModel *model = self.dataArray.firstObject;

    if (tableView == self.tab1) {
        
        if (indexPath.row == 2) {
            
            CGFloat H = [OAToolBox sizebyText:model.CONTENTS font:15 minsWidth:200];
            
            return H + 50;
        }else{
            
            return 60;
        }
        
    }else{
    
        return 165;
    }

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (tableView == self.tab2 && section == 0) {

        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width - 20, 40)];
        [self.view addSubview:view1];
        view1.backgroundColor = [UIColor whiteColor];
        
        UIView *view2 = [[UIView alloc] init];
        [view1 addSubview:view2];
        view2.backgroundColor = [UIColor blueColor];
        [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(view1.mas_top).mas_offset(5);
            make.left.equalTo(view1.mas_left).mas_offset(5);
            make.width.equalTo(@4);
            make.bottom.equalTo(view1.mas_bottom).mas_offset(-5);
        }];
        
        UILabel *lbl = [[UILabel alloc] init];
        lbl.tintColor = [UIColor blueColor];
        [view1 addSubview:lbl];
        lbl.text = @"成员列表";
        [lbl setFont:[UIFont systemFontOfSize:13]];
        [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view1.mas_top).mas_offset(5);
            make.left.equalTo(view2.mas_right).mas_offset(5);
            make.bottom.equalTo(view1.mas_bottom).mas_offset(-5);
            
        }];
        
        return view1;
    }else{
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width - 20, 40)];
        [self.view addSubview:view1];
        view1.backgroundColor = [UIColor whiteColor];
        
        UIView *view2 = [[UIView alloc] init];
        [view1 addSubview:view2];
        view2.backgroundColor = [UIColor blueColor];
        [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(view1.mas_top).mas_offset(5);
            make.left.equalTo(view1.mas_left).mas_offset(5);
            make.width.equalTo(@4);
            make.bottom.equalTo(view1.mas_bottom).mas_offset(-5);
        }];
        
        UILabel *lbl = [[UILabel alloc] init];
        lbl.tintColor = [UIColor blueColor];
        [view1 addSubview:lbl];
        lbl.text = @"考察信息";
        [lbl setFont:[UIFont systemFontOfSize:13]];
        [lbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view1.mas_top).mas_offset(5);
            make.left.equalTo(view2.mas_right).mas_offset(5);
            make.bottom.equalTo(view1.mas_bottom).mas_offset(-5);
            
        }];

        return view1;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (tableView == self.tab2) {
        return 20.0;
    }else{
        return 20;
    }
    
}

//实现这个方法  才会掉用viewForHeaderInSection
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (tableView == self.tab2) {
        return @"成员列表";
    }else{
        return @"考察信息";
    }
    
}


@end

















